<template>
  <div class="ant-step-item" :class="statusClass" :style="itemStyle">
    <div class="ant-step-item-container">
      <div class="ant-step-item-line-container">
        <div class="ant-step-item-head-container">
          <div style="height: 100%">
            <div class="ant-step-item-icon">
              <slot name="icon">
                <span v-if="!isFinish">{{ index + 1 }}</span>
                <span v-else>✓</span>
              </slot>
              <!-- 水平模式下的标题容器 -->
              <template v-if="!isVertical">
                <div class="ant-step-item-title-container">
                  <slot name="title">
                    <div class="ant-step-item-title">{{ title }}</div>
                  </slot>
                </div>
              </template>
            </div>

            <div class="ant-step-item-line-column" v-if="isVertical">
              <template>
                <div class="ant-step-item-description-container">
                  <slot name="description">
                    <div v-if="description" class="ant-step-item-description">
                      {{ description }}
                    </div>
                  </slot>
                </div>
              </template>
            </div>
          </div>

          <!-- 垂直模式下，标题和描述放在图标右边 -->
          <template v-if="isVertical">
            <div class="ant-step-item-content">
              <slot name="title">
                <div class="ant-step-item-title">{{ title }}</div>
              </slot>
              <slot name="description">
                <div v-if="description" class="ant-step-item-description">{{ description }}</div>
              </slot>
            </div>
          </template>
        </div>
        <div class="ant-step-item-line" v-if="!isVertical">
          <template>
            <div class="ant-step-item-description-container">
              <slot name="description">
                <div v-if="description" class="ant-step-item-description">{{ description }}</div>
              </slot>
            </div>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { inject, computed, ref } from 'vue'

const props = defineProps({
  title: String,
  description: String,
  icon: String,
  processColor: {
    type: String,
    default: '#1677ff',
  },
  finishColor: {
    type: String,
    default: '#1677ff',
  },
  errorColor: {
    type: String,
    default: '#ff4d4f',
  },
  waitColor: {
    type: String,
    default: '#d9d9d9',
  },
})

const stepsContext = inject('stepsContext')
const getCurrentIndex = inject('getCurrentIndex')
const index = ref(getCurrentIndex()) // 获取当前步骤的索引

// 修改 stepIndex 的注入方式
const stepIndex = computed(() => index.value)

const isVertical = computed(() => stepsContext.direction.value === 'vertical')

const currentStatus = computed(() => {
  const current = stepsContext.current.value
  const status = stepsContext.status.value

  if (stepIndex.value < current) {
    return 'finish'
  } else if (stepIndex.value === current) {
    return status
  }
  return 'wait'
})

const isProcess = computed(() => currentStatus.value === 'process')
const isFinish = computed(() => currentStatus.value === 'finish')

const statusClass = computed(() => ({
  [`ant-step-item-${currentStatus.value}`]: true,
  'ant-step-item-custom': !!props.icon,
  'ant-steps-small': stepsContext.size.value === 'small',
}))

const itemStyle = computed(() => {
  if (currentStatus.value === 'error') return { '--status-color': props.errorColor }
  if (isProcess.value) return { '--status-color': props.processColor }
  if (isFinish.value) return { '--status-color': props.finishColor }
  return { '--status-color': props.waitColor }
})
</script>

<style scoped>
.ant-step-item {
  flex: 1;
  height: 100%;
  position: relative;
  vertical-align: top;
  padding-bottom: 40px; /* 为标题预留空间 */
}

.ant-step-item-container {
  display: flex;
  align-items: flex-start;
  height: 100%;
}

.ant-step-item-line-container {
  display: flex;
  align-items: center;
  flex: 1;
  height: 100%;
}

.ant-step-item-head-container {
  display: flex;
  align-items: flex-start;
  height: 100%;
}

.ant-step-item-line {
  position: relative;
  flex: 1;
  height: 1px;
  background-color: #d9d9d9;
  margin-left: 8px;
  margin-right: 8px;
}

.ant-step-item-line-column {
  position: absolute;
  left: 14px; /* 图标宽度的一半，使线条位于图标中心 */
  top: 28px; /* 从图标底部开始 */
  height: calc(100% - 28px); /* 减去图标高度 */
  width: 1px;
  background-color: #d9d9d9;
}
/* 水平模式下的标题容器新样式 */
.ant-step-item-title-container {
  position: absolute;
  top: 32px;
  text-align: center;
  min-width: 100px;
}

/* 水平模式下的描述容器 */
.ant-step-item-description-container {
  position: absolute;
  top: 24px;
  left: 0;
  width: 100%;
  text-align: center;
}

/* 垂直模式样式 */
.ant-steps-vertical .ant-step-item-line-container {
  display: block; /* 改为块级元素 */
  position: relative;
  flex: none; /* 移除 flex 属性 */
}

.ant-steps-vertical .ant-step-item-head-container {
  display: flex;
  align-items: flex-start;
  position: relative;
}

.ant-steps-vertical .ant-step-item-content {
  margin-left: 12px;
  min-height: 48px;
}

.ant-steps-vertical .ant-step-item-line {
  position: absolute;
  left: 14px; /* 圆圈直径28px的一半加上一点偏移 */
  top: 28px; /* 圆圈的高度 */
  width: 1px;
  height: calc(100% - 14px);
  margin: 0;
  background-color: #d9d9d9;
}

.ant-steps-vertical .ant-step-item {
  display: block;
  flex: 1 0 auto;
  padding-bottom: 0;
  position: relative;
}

.ant-steps-vertical .ant-step-item:not(:last-child) {
  /* margin-bottom: 16px; */
}

/* 图标样式 */
.ant-step-item-icon {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #fff;
  border: 1px solid #d9d9d9;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  z-index: 1;
}

/* 标题样式 */
.ant-step-item-title {
  color: rgba(0, 0, 0, 0.88);
  font-size: 14px;
  line-height: 24px;
  position: relative;
}

/* 描述文本样式 */
.ant-step-item-description {
  color: rgba(0, 0, 0, 0.45);
  font-size: 12px;
  margin-top: 4px;
}

/* 状态样式 */
.ant-step-item-finish .ant-step-item-icon {
  border-color: var(--status-color);
  background: var(--status-color);
  color: white;
}

.ant-step-item-error .ant-step-item-icon {
  border-color: var(--status-color);
  color: var(--status-color);
}

.ant-step-item-process .ant-step-item-icon {
  border-color: var(--status-color);
  color: var(--status-color);
}

/* 线条颜色也可以跟随状态 */
.ant-step-item-finish .ant-step-item-line,
.ant-step-item-finish .ant-step-item-line-column {
  background-color: var(--status-color);
}

/* 最后一个步骤不显示连接线 */
.ant-step-item:last-child .ant-step-item-line,
.ant-step-item:last-child .ant-step-item-line-column {
  display: none;
}
</style>
